﻿//
// Created by francklinson on 2021/3/22.
//

#include "Time.h"

Time::Time()
{
    hours = minutes = 0;
}

Time::Time(int h, int m)
{
    hours = h;
    minutes = m;
}

void Time::AddMin(int m)
{
    minutes += m;
    hours += minutes / 60;
    minutes %= 60;
}

void Time::AddHr(int h)
{
    hours += h;
}

void Time::Reset(int h, int m)
{
    hours = h;
    minutes = m;
}

Time Time::operator+(const Time &t) const
{
    Time sum;
    sum.minutes = this->minutes + t.minutes;
    sum.hours = this->hours + t.hours + sum.minutes / 60;
    sum.minutes %= 60;
    return sum;
}

Time Time::operator-(const Time &t) const
{
    Time ret;
    int tot1, tot2;
    tot1 = this->hours * 60 + this->minutes;
    tot2 = t.hours * 60 + t.minutes;
    ret.minutes = (tot1 - tot2) % 60;
    ret.hours = (tot1 - tot2) / 60;
    return ret;
}

Time Time::operator*(double n) const
{
    Time result;
    long totalMinutes = hours * n * 60 + minutes * n;
    result.hours = totalMinutes / 60;
    result.minutes = totalMinutes % 60;
    return result;
}

// 友元重载版本
Time operator*(double m, const Time &t)
{
    Time result;
    long totalMinutes = t.hours * m * 60 + t.minutes * m;
    result.hours = totalMinutes / 60;
    result.minutes = totalMinutes % 60;
    return result;
}

void Time::Show() const
{
    std::cout << hours << " hours, " << minutes << " minutes";
}

std::ostream &operator<<(std::ostream &os, const Time &t)
{
    os << t.hours << " hours, " << t.minutes << " minutes";
    return os;
}
